let express = require('express')

//got模块，相当于前台的ajax功能
let got = require('got')

let app = express()

app.get('/login',(req,res)=>{
    let js_code = req.query.code //这是小程序进行登录临时的code  5分钟
    let appid = 'wx6ec4f2956a837aef'
    let secret = 'eace7cf60feb7f919ebcba3eb5b428d3' //此小程序appid的密钥  从小程序后台 。
    let code2sessionUrl = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${js_code}&grant_type=authorization_code`
    // console.log(code2sessionUrl);

    ;(async () => {
        try {
            const response = await got( code2sessionUrl );//发送got请求
            // console.log(response);exit();
            let result = JSON.parse( response.body )
            // console.log(result);exit();
            res.send({
                openid:result.openid
            })
            
        } catch (error) {
            res.send(error.response.body)
        }
    })()
})


app.get('/checklogin',(req,res)=>{
    //req.query.openid + 'woshiyigesuozi'
})

app.listen(3000,()=>{
    console.log("---3000--")
})

